<!-- the actual form, for normal order -->
<?php
function setVariables($id){
    $array['id'] = $id;
    $array['number'] = $_POST['number']; //product number
    $array['amount'] = $_POST['amount'];
    $array['name'] = $_POST['name'];
    $array['type'] = $_POST['type'];
    $array['price'] = $_POST['price'];
    $array['VAT'] = $_POST['VAT'];
    //if discount is empty make it 0
    if(empty($_POST['discount'])){
        $array['discount'] = 0;
    } else {
        $array['discount'] = $_POST['discount'];
    }    
    $i = 0;
    while(isset($_SESSION['stockOrderlist'][$i])){                  
        $i++;
    }
    $_SESSION['stockOrderlist'][$i] = $array;
    unset($_POST['add']);
}
//if empty button is clicked empty the orderlist
if(isset($_POST['empty_order'])){
    if(isset($_SESSION['stockOrderlist'])){
        unset($_SESSION['stockOrderlist']);
    }
}

//if delete button is clocked remove product from session
if(isset($_POST['removep'])){
    $idline = $_POST['removep'];
    unset($_SESSION['stockOrderlist'][$idline]);
}

//if update is clicked, update that product
if(isset($_POST['update'])){
    $i = 0;
    while(isset($_POST[''.$i.''])){
        $_SESSION['stockOrderlist'][$i]['amount'] = $_POST['amount'.$i.''];
        $_SESSION['stockOrderlist'][$i]['discount'] = $_POST['discount'.$i.''];
        $i++;
    }
    unset($_POST['update']);
}
//if add button is clicked add new product to the session
if(isset($_POST['addp'])) {
    //check if something is filled, if not, do nothing
    if(!empty($_POST['amount'])){
        $id = $_POST['id'];
        if(isset($_SESSION['stockOrderlist'])){
            $inlist = false; //standard inlist is false
            foreach($_SESSION['stockOrderlist'] as $value){ //go through list
                //if id is the same AND category is the same the product or part is already in the list
                if($id == $value['id']){
                    $inlist = true; //the product is already in the session
                    echo '<span style="color:red;">The item has already been added to the list</span>';
                    break;//get out of for each loop
                }//close if
            }//close foreach
            if($inlist == false){
                setVariables($id);
            }
        } else {
            setVariables($id);
        }
    }
    else{
       echo '<span style="color:red;">Please enter an quantity</span>'; 
    }
}
?>
<form action="<?php echo url_for('sales/' . ($form->getObject()->isNew() ? 'createStockOrder' : 'updateStockOrder') . (!$form->getObject()->isNew() ? '?id=' . $form->getObject()->getId() : '')) ?>" method="post" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?>>    
    <?php if (!$form->getObject()->isNew()): ?>
        <input type="hidden" name="sf_method" value="put" />
    <?php endif; ?>
    <!-- general information needed for an order -->
    <fieldset>
        <legend class="bold">General information</legend>
        <p style="color:red;">Items with * are required to be filled in</p>
        <table>
            <?php $form->customizeStatus($status, $category);
                  $form->setCategory($category); 
                  if(!$form->getObject()->isNew()) {?>
            <tr>
                <td class="left_title italic">Status: *</td>
                <td class="left_input"><?php echo $form['status']->render() ?></td>
                <td class="left_error"><?php echo $form['status']->renderError() ?></td>
            </tr>
            <?php } ?>
            <tr>
                <td class="left_title italic">Due date: *</td>
                <td class="left_input"><?php echo $form['date_due']->render() ?></td>
                <td class="left_error"><?php echo $form['date_due']->renderError() ?></td>
            </tr>
        </table>
    </fieldset>
    <fieldset>
        <legend class="bold">Contact</legend>
        <table>
            <tr>
                <td class="left_title italic">Contact: *</td>
                <td class="left_input"><?php echo $form['contact_id']->render() ?></td>
                <td class="left_error"><?php echo $form['contact_id']->renderError() ?></td>
            </tr>
        </table>
        <!-- search contact -->
    </fieldset>
    <?php    //check if the order is new
    if($form->getObject()->isNew()){ ?>
    <fieldset>
        <legend class="bold">Order</legend>
        <table class="table_top">
            <tr class="bold head_small">
                <td width="10%">Item No.</td>
                <td width="20%">Name</td>
                <td>Type</td>
                <td width="100px">Price per Unit (excl. VAT)</td>
                <td width="40px">VAT(%)</td>
                <td width="8%">Quantity</td>
                <td width="8%">Discount(%)</td> 
                <td width="35px"></td>
            </tr>
        </table>
        <div id="scroll_div" class="table_div">    
            <table id="order" class="table_hov">
                <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" style="visibility:hidden;">
                    <input type="hidden" value="Add" name="retp"/>
                </form>
                <!-- ugly solution to get the table right -->
                <tr class="bold head_small" height="15px">
                    <td width="10%"></td>
                    <td width="20%"></td>
                    <td></td>
                    <td width="100px"></td>
                    <td width="40px"></td>
                    <td width="8%"></td>
                    <td width="8%"></td> 
                    <td width="20px"></td>
                </tr>
                <?php foreach ($products as $i=>$product): ?>
                    <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> ">
                        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                            <td><?php echo $product->getProductNumber() ?></td>
                            <td><?php echo $product->getName() ?></td>
                            <td><?php echo $product->getType() ?></td>
                            <td><?php echo $product->getPrice() ?></td>
                            <td><?php echo $product->getVAT() ?></td>
                            <input type="hidden" name="id" value="<?php echo $product->getId() ?>"/>
                            <input type="hidden" name="name" value="<?php echo $product->getName() ?>"/>
                            <input type="hidden" name="price" value="<?php echo $product->getPrice() ?>"/>
                            <input type="hidden" name="VAT" value="<?php echo $product->getVAT() ?>"/>
                            <input type="hidden" name="type" value="<?php echo $product->getType() ?>"/>
                            <input type="hidden" name="number" value="<?php echo $product->getProductNumber() ?>"/>
                            <td><input type="text" name="amount" size="3"/></td>
                            <td><input type="text" name="discount" size="3" value="0"/></td>
                            <td><input type="submit" class="add_bt" value="" name="addp" title="Add to order"/></td>
                        </form>
                    </tr>
                <?php endforeach; ?>
            </table>
        </div>
    </fieldset>
<?php if(!empty($_SESSION['stockOrderlist'])){ //check if there is something in the orderlist ?>
    <fieldset>
        <legend class="bold">Order List</legend>
        <div class="table_div">
        <table id="orderlist">
            <tr class="bold head_small">
                <td>Item No.</td>
                <td>Name</td>
                <td>Type</td>
                <td>Price per Unit (excl. VAT)</td>
                <td>VAT(%)</td>
                <td>Quantity</td>
                <td>Discount(%)</td>
                <td width="8%">Total price</td>
                <td width="20px;"></td>
            </tr>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"><!-- empty the session -->
                <input type="submit" name="empty_order" value="" class="bt_empty" title="Empty entire order list"/>
            </form>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
                <input type="submit" name="update" value="" class="bt_update" title="Update order list"/><!-- update the session -->
                <?php
                $totalprice = 0;
                foreach($_SESSION['stockOrderlist'] as $value){ ?>
                    <tr class="table_hov <?php echo fmod($i, 2) ? 'even' : 'odd' ?> "><?php
                    echo '<td>'.$value['number'].'</td>';
                    echo '<td>'.$value['name'].'</td>';
                    echo '<td>'.$value['type'].'</td>';
                    echo '<td>'.$value['price'].'</td>';
                    echo '<td>'.$value['VAT'].'</td>';
                    echo '<input type="hidden" name="id" value="'.$value['id'].'"/>';
                    echo '<input type="hidden" name="name" value="'.$value['name'].'"/>';
                    echo '<input type="hidden" name="price" value="'.$value['price'].'"/>';
                    echo '<input type="hidden" name="type" value="'.$value['type'].'"/>';
                    echo '<input type="hidden" name="number" value="'.$value['number'].'"/>';
                    echo '<input type="hidden" name="'.array_search($value, $_SESSION['stockOrderlist']).'" value="'.array_search($value, $_SESSION['stockOrderlist']).'"/>';
                    echo '<td><input type="text" value="'.$value['amount'].'" name="amount'.array_search($value, $_SESSION['stockOrderlist']).'" size="3"/></td>';
                    echo '<td><input type="text" name="discount'.array_search($value, $_SESSION['stockOrderlist']).'" size="5" value="'.$value['discount'].'"/></td>';                      
                    if($value['amount'] != '0'){
                        $discount = 1 - ($value['discount'] / 100);
                        $total = $value['amount'] * ($value['price'] * $discount);
                        $totalprice += $total;
                        echo '<td>'.$total.'</td>';
                        if(!isset($VAT_order)){
                            $VAT_order = 0;
                        }
                        $VAT = ($value['VAT'] / 100);
                        $VAT_prod = $VAT * $value['price'];
                        $VAT_order = $VAT_order + $VAT_prod;
                        
                    } else { 
                        echo '<td>0</td>'; 
                    }
                        echo '<td><input type="submit" value="'.array_search($value, $_SESSION['stockOrderlist']).'" name="removep" class="bt_delete" title="Remove from order"/></td>';
                        echo '</tr>';
                } ?>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td><?php echo $totalprice ?></td>
                </tr>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td class="bold">VAT:</td>
                    <td><?php echo $VAT_order ?></td>
                </tr>
                <?php
                    $totalprice = $totalprice + $VAT_order;
                ?>
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td class="bold">Total:</td>
                    <td><?php echo $totalprice ?></td>
                    <?php $form->setTotalPrice($totalprice); ?>
                </tr>
            </form>            
        </table>
        </div>
            </fieldset>
            <div class="hidden_csrf">
                <?php echo $form['_csrf_token']->renderRow(); ?>
                <?php echo $form['date_recieved']->renderRow(); ?>
                <?php echo $form['total_price']->renderRow(); ?>
                <?php echo $form['category_name']->renderRow(); ?>
                <?php echo $form['status']->renderRow(); ?>
            </div>
            <input type="submit" value="" class="ok ok_margin" title="Confirm order"/>
            <?php                
        }    
    } else { ?>
        <div class="hidden_csrf">
                <?php echo $form['_csrf_token']->renderRow(); ?>
                <?php echo $form['date_recieved']->renderRow(); ?>
                <?php echo $form['total_price']->renderRow(); ?>
                <?php echo $form['date_ready']->renderRow(); ?>
                <?php echo $form['category_name']->renderRow(); ?>
        </div>
        <input type="submit" value="" class="ok" title="Confirm order"/>
        <div style="margin: -35px 0 0 80px;">
        <a href="<?php echo url_for('sales_print_order', $form->getObject()) ?>">
            <img src="<?php echo image_path('Buttons/bt_print.png') ?>" onmouseover="this.src='<?php echo image_path('Buttons/bt_print_hov.png') ?>';" onmouseout="this.src='<?php echo image_path('Buttons/bt_print.png') ?>';" title="Print documents"/>
        </a>
        </div>
    <?php }
    ?>      
</form>
